/**
Copyright (c) 2008 Arnaud Fabre <arnaud333@gmail.com>
Copyright (c) 2008 Pierre "delroth" Bourdon <root@delroth.is-a-geek.org>

This file is part of Vos Tutos Offline.

Vos Tutos Offline is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Vos Tutos Offline is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Vos Tutos Offline.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "treeview.h"

ICTreeItem::ICTreeItem(IntroducedDocument* docp, int type) : QTreeWidgetItem(type)
{
    parent = docp; this->type = type;
}

ICTreeItem::ICTreeItem(const QStringList&l,
		       IntroducedDocument* docp, int type) : QTreeWidgetItem(l, type)
{
    parent = docp; this->type = type;
}

IntroducedDocument* ICTreeItem::getParent() const
{
    return this->parent;
}

int ICTreeItem::getType() const
{
    return this->type;
}

void ICTreeItem::setParent(IntroducedDocument* p)
{
    parent = p;
}

void ICTreeItem::setType(int t)
{
    type = t;
}

/* TutoTreeItem */

TutoTreeItem::TutoTreeItem(TitledDocument* docp, int type) : QTreeWidgetItem(type)
{
    part = docp; this->type = type;
}

TutoTreeItem::TutoTreeItem(const QStringList& l,
			   TitledDocument* docp, int type) : QTreeWidgetItem(l, type)
{
    part = docp; this->type = type;
}

TitledDocument* TutoTreeItem::getPart() const
{
    return part;
}

int TutoTreeItem::getType() const
{
    return type;
}

void TutoTreeItem::setPart(TitledDocument* p)
{
    part = p;
}

void TutoTreeItem::setType(int t)
{
    type = t;
}

TutoTree::TutoTree(TitledDocument* doc, int type, QWidget* parent) :
    QTreeWidget(parent)
{
    this->doc = doc;
    setColumnCount(1);
    setType(type);
    setHeaderLabel(tr("Document explorer"));
    refresh();
}

int TutoTree::getType() const
{
    return this->type;
}

void TutoTree::setType(int type)
{
    this->type = type;
}

void TutoTree::refresh()
{
    if(type == NEWS) refreshPart();
    else if(type == MINITUTORIAL) refreshMiniTutorial();
    else if(type == BIGTUTORIAL) refreshBigTutorial();
}

TutoTreeItem* TutoTree::getClearedRoot(int type)
{
    //QTreeWidgetItem* r = headerItem();
    //for(int i = 0; i < r->childCount(); ++i)
    //    r->removeChild(r->child(i));
    TutoTreeItem* foo = new TutoTreeItem(NULL, type);
    takeTopLevelItem(0);
    addTopLevelItem(foo);
    return foo;
}

void TutoTree::refreshPart()
{
    Part* news = static_cast<Part*>(doc);
    TutoTreeItem* r = getClearedRoot(NEWS);
    r->setText(0, news->getTitle());
    r->setPart(news);
    r->setType(NEWS);
}

void TutoTree::refreshMiniTutorial()
{
    MiniTutorial* tuto = static_cast<MiniTutorial*>(doc);
    TutoTreeItem* r = getClearedRoot(MINITUTORIAL);

    r->setText(0, tuto->getTitle());
    r->setPart(tuto);
    r->setType(MINITUTORIAL);
    
    r->addChild(new ICTreeItem(QStringList(tr("Introduction")), 
				   tuto,INTRO));

    QList<Part*> parts = tuto->allParts();
    for(QList<Part*>::iterator it = parts.begin(); it != parts.end(); ++it)
        r->addChild(
                new TutoTreeItem(QStringList((*it)->getTitle()), *it, NEWS)
                );
                
    r->addChild(new ICTreeItem(QStringList(tr("Conclusion")), 
			       tuto,CONCLU));
}

void TutoTree::refreshBigTutorial()
{

    BigTutorial* tuto = static_cast<BigTutorial*>(doc);
    TutoTreeItem* r = getClearedRoot(BIGTUTORIAL);
  	  	
    r->setText(0, tuto->getTitle());
    r->setPart(tuto);
    r->setType(BIGTUTORIAL);
	
    r->addChild(new ICTreeItem(QStringList(tr("Introduction")),
			       tuto,INTRO));
	
    QList<BigTutorialPart*> ps = tuto->allParts();
    for(QList<BigTutorialPart*>::iterator it = ps.begin();
            it != ps.end(); ++it)
    {
        QList<MiniTutorial*> mtut = (*it)->allChapters();
        TutoTreeItem* parti = new TutoTreeItem(
                QStringList((*it)->getTitle()), *it, CHAPTER);
        r->addChild(parti);

        parti->addChild(new ICTreeItem(QStringList(tr("Introduction")),
				      *it, INTRO));
        
        for(QList<MiniTutorial*>::iterator partit = mtut.begin();
                partit != mtut.end(); ++partit)
        {
            QList<Part*> parts = (*partit)->allParts();
            TutoTreeItem* tuti = new TutoTreeItem(
                    QStringList((*partit)->getTitle()), *partit,
                    MINITUTORIAL);
            parti->addChild(tuti);
	    tuti->addChild(new ICTreeItem(QStringList(tr("Introduction")),
					  *partit,INTRO));

            for(QList<Part*>::iterator tutit = parts.begin();
                    tutit != parts.end(); ++tutit)
                tuti->addChild(
                        new TutoTreeItem(
                            QStringList((*tutit)->getTitle()),
                            *it, NEWS
                            )
                        );
	    tuti->addChild(new ICTreeItem(QStringList(tr("Conclusion")),
					  *partit,CONCLU));
        }
	parti->addChild(new ICTreeItem(QStringList(tr("Conclusion")),
				       *it, CONCLU));
    }
    r->addChild(new ICTreeItem(QStringList(tr("Conclusion")),
			       tuto,CONCLU));

}

